//
//  RightViewController.m
//  侧栏
//
//  Created by Pinken on 2018/5/31.
//  Copyright © 2018年 RX. All rights reserved.
//

#import "RightViewController.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

#define kCVWidth ScreenWidth * 2.2/5

@interface RightViewController ()

// 蒙版
@property (nonatomic, strong) UIView *maskView;

// 内容部分
@property (nonatomic, strong) UIView *contentView;

@end

@implementation RightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUI];
}

- (void)setUI {
    // 按钮，点击可回到Main 控制器
    UIButton *button = [[UIButton alloc] init];
    button.frame = CGRectMake(0, 0, ScreenWidth-kCVWidth, ScreenHeight);
//    button.backgroundColor = [UIColor redColor];
//    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [button setTitle:@"right 中的btn" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickRightCtrBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    // 蒙版
    self.maskView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.maskView.alpha = 0;
    self.maskView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_maskView];
    [self.view sendSubviewToBack:self.maskView];
    
    // 内容 view
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth, 0, kCVWidth, ScreenHeight)];
    self.contentView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.contentView];
    
    // 蒙版和内容出现动画
    [UIView animateWithDuration:0.5 animations:^{
        self.maskView.alpha = 0.6;
        self.contentView.frame = CGRectMake(ScreenWidth-kCVWidth, 0, kCVWidth, ScreenHeight);
    }];
}

- (void)clickRightCtrBtn {
    NSLog(@"点击了右侧栏控制器中的按钮 --- ");
    
    // 蒙版和内容消失动画
    [UIView animateWithDuration:0.5 animations:^{
        self.maskView.alpha = 0;
        self.contentView.frame = CGRectMake(ScreenWidth, 0, kCVWidth, ScreenHeight);
        
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
    
//    [self.view removeFromSuperview];
//    [self removeFromParentViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
